/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.minierp.bean.UserBean;
import com.minierp.bean.UserStatus;
import com.minierp.dao.TallySetDao;
import com.minierp.err.PermissionDennyException;
import com.minierp.helper.EncCommonHelper;
import com.minierp.helper.EncInterceptorHelper;
import com.minierp.helper.PageHelper;
import com.minierp.model.TallySet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class TallySetMACtr extends MultiActionController {
	private TallySetDao tallySetDao;
	private String listView;

	public ModelAndView handleList(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		if (!EncCommonHelper.checkPrivilege(request, "SYST_TS")) {
			throw new PermissionDennyException("Permission Denny");
		} else {
			ModelAndView mv = new ModelAndView(this.listView);
			List list = this.tallySetDao.getList(false);
			mv.addObject("list", list);
			return mv;
		}
	}

	public ModelAndView handleDelete(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String retUrl = request.getParameter("from_url");
		ModelAndView mv = new ModelAndView("redirect:" + retUrl);
		TallySet p = (TallySet) this.tallySetDao.get(TallySet.class,
				Long.parseLong(request.getParameter("id")));
		if (p != null && p.getSys().equals("N")) {
			p.setEnabled("N");
			this.tallySetDao.update(p);
			EncCommonHelper.setAlertMessage(request, "删除成功！");
		}

		return mv;
	}

	public ModelAndView handlePause(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String retUrl = request.getParameter("from_url");
		String flag = request.getParameter("flag");
		ModelAndView mv = new ModelAndView("redirect:" + retUrl);
		TallySet p = (TallySet) this.tallySetDao.get(TallySet.class,
				Long.parseLong(request.getParameter("id")));
		if (p != null) {
			if (flag != null && flag.equals("P")) {
				p.setEnabled("P");
			} else {
				p.setEnabled("Y");
			}

			this.tallySetDao.update(p);
		}

		EncCommonHelper.setAlertMessage(request, "设置成功！");
		return mv;
	}

	public ModelAndView handleCheck(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		HashMap map = new HashMap();

		String json;
		try {
			String mapper = request.getParameter("ts_name");
			json = request.getParameter("exp_id");
			List out = this.tallySetDao.getList(mapper, Long.parseLong(json));
			if (out.size() > 0) {
				map.put("status", "EXIST");
			} else {
				map.put("status", "NO_EXIST");
			}

			map.put("errMsg", "");
		} catch (Exception arg6) {
			map.put("status", "ERROR");
			map.put("errMsg", arg6.toString());
		}

		response.setContentType("text/plain");
		response.setCharacterEncoding("utf-8");
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Pragma", "no-cache");
		ObjectMapper mapper1 = new ObjectMapper();
		json = mapper1.writeValueAsString(map);
		ServletOutputStream out1 = response.getOutputStream();
		out1.write(json.getBytes("utf-8"));
		out1.flush();
		out1.close();
		return null;
	}

	public ModelAndView handleRecover(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		HashMap map = new HashMap();
		String tsName = request.getParameter("ts_name");
		this.tallySetDao.recover(tsName);
		map.put("status", "OK");
		EncCommonHelper.setAlertMessage(request, "恢复成功！");
		response.setContentType("text/plain");
		response.setCharacterEncoding("utf-8");
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Pragma", "no-cache");
		ObjectMapper mapper = new ObjectMapper();
		String json = mapper.writeValueAsString(map);
		ServletOutputStream out = response.getOutputStream();
		out.write(json.getBytes("utf-8"));
		out.flush();
		out.close();
		return null;
	}

	public ModelAndView handleAppList(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		HashMap map = new HashMap();
		UserStatus s = EncInterceptorHelper.checkAppUserSignon(request);
		UserBean ub = s.getUserBean();
		if (ub == null) {
			map.put("status", "NO_LOGIN");
			map.put("errMsg", "");
		} else {
			try {
				List mapper = PageHelper.getTallySetList(this.tallySetDao, ub);
				ArrayList json = new ArrayList();

				for (int out = 0; out < mapper.size(); ++out) {
					HashMap item = new HashMap();
					TallySet b = (TallySet) mapper.get(out);
					item.put("id", Long.valueOf(b.getId()));
					item.put("name", b.getTitle());
					json.add(item);
				}

				map.put("items", json);
				map.put("defa", Long.valueOf(ub.getUser().getDfTsId()));
				map.put("status", "OK");
				map.put("errMsg", "");
			} catch (Exception arg10) {
				map.put("status", "ERROR");
				map.put("errMsg", arg10.toString());
			}
		}

		response.setContentType("text/plain");
		response.setCharacterEncoding("utf-8");
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Pragma", "no-cache");
		ObjectMapper arg11 = new ObjectMapper();
		String arg12 = arg11.writeValueAsString(map);
		ServletOutputStream arg13 = response.getOutputStream();
		arg13.write(arg12.getBytes("utf-8"));
		arg13.flush();
		arg13.close();
		return null;
	}

	public String getListView() {
		return this.listView;
	}

	public void setListView(String listView) {
		this.listView = listView;
	}

	public TallySetDao getTallySetDao() {
		return this.tallySetDao;
	}

	public void setTallySetDao(TallySetDao tallySetDao) {
		this.tallySetDao = tallySetDao;
	}
}